package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author faiz
 * @since 2021-04-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneSubject() {
        //查询所有的一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> eduOneSubjectList = baseMapper.selectList(wrapperOne);
        //查询所有的二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> eduTwoSubjectList = this.list(wrapperTwo);


        //封装一级分类
//        for (EduSubject eduOneSubject: eduOneSubjectList) {
//            OneSubject oneSubject = new OneSubject();
//            //与上面的代码功能类似
//            BeanUtils.copyProperties(eduOneSubject, oneSubject);
//            //在一级分类的循环中遍历所有的二级分类
//            //创建list集合封装每个一级分类的二级分类
//            List<TwoSubject> twoSubjectList = new ArrayList<>();
//            //遍历二级分类list集合
//            for (EduSubject eduTwoSubject: eduTwoSubjectList) {
//                //获取每个二级分类
//                //判断二级分类parentId和一级分类的id是否一样
//                if (eduTwoSubject.getParentId().equals(eduOneSubject.getId())) {
//                    //复制
//                    TwoSubject twoSubject = new TwoSubject();
//                    BeanUtils.copyProperties(eduTwoSubject, twoSubject);
//                    twoSubjectList.add(twoSubject);
//                }
//            }
//            oneSubject.setChildren(twoSubjectList);
//            finalSubjectList.add(oneSubject);
//        }
        List<OneSubject> finalSubjectList = eduOneSubjectList.stream().map(item -> {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(item, oneSubject);
            return oneSubject;
        }).map(item -> {
                    List<TwoSubject> twoSubjectList = eduTwoSubjectList.stream()
                            .filter(item2 -> item.getId().equals(item2.getParentId()))
                            .map(item2 -> {
                                TwoSubject twoSubject = new TwoSubject();
                                BeanUtils.copyProperties(item2, twoSubject);
                                return twoSubject;
                            }).collect(Collectors.toList());
                    item.setChildren(twoSubjectList);
                    return item;
                }
        ).collect(Collectors.toList());

        return finalSubjectList;
    }
}
